package com.yspa.manual.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.util.Pagination;
import com.yspa.manual.dao.ChildManualDao;
import com.yspa.manual.model.form.ChildManualForm;

@Repository
public class ChildManualDaoImpl implements ChildManualDao {

	@Autowired
	private HibernateSqlUtil dao;
	public Pagination getChildManualList(Pagination page){
		StringBuffer sql = new StringBuffer();
		sql.append("select * from child_manual")
		.append(" where 1=1 ");
		Map<String, Object> params = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(page.getConditions().get("title"))){
			sql.append(" and title like :title");
			params.put("title", "%" + page.getConditions().get("title").trim() + "%");
		}
		if (StringUtils.isNotBlank(page.getConditions().get("satart_age"))){
			sql.append(" and satart_age >= :satart_age");
			params.put("satart_age",  page.getConditions().get("satart_age").trim() );
		}
		if (StringUtils.isNotBlank(page.getConditions().get("end_age"))){
			sql.append(" and end_age <= :end_age");
			params.put("end_age",  page.getConditions().get("end_age").trim() );
		}
		if (StringUtils.isNotBlank(page.getConditions().get("mtype"))){
			sql.append(" and mtype = :mtype");
			params.put("mtype",  page.getConditions().get("mtype").trim() );
		}
		return dao.getSqlPageMapList(sql.toString(), params, page);
	}
	public void addChildManual(ChildManualForm form){
		StringBuffer sql = new StringBuffer();
		sql.append("insert into child_manual(id,title,content,bucd,create_time,url,satart_age,end_age,mtype)")
		.append(" value(:id,:title,:content,:bucd,sysdate(),:url,:satart_age,:end_age,:mtype)");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", UUID.randomUUID().toString());
		params.put("title", form.getTitle());
		params.put("content", form.getContent());
		params.put("bucd", "wkhm_c");
		params.put("url", form.getUrl());
		params.put("satart_age", form.getSatart_age());
		params.put("end_age", form.getEnd_age());
		params.put("mtype", form.getMtype());
		dao.executeSql(sql.toString(), params);
	}
	public List getEditInfo(String id){
		String sql = "select * from child_manual where id=:id";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		return dao.getSqlMapList(sql, params);
	}
	public void editInfo(ChildManualForm form){
		StringBuffer sql = new StringBuffer();
		sql.append("update child_manual")
		.append(" set title=:title,")
		.append("content=:content,")
		.append("url=:url,")
		.append("satart_age=:satart_age,")
		.append("end_age = :end_age,")
		.append("mtype = :mtype")
		.append(" where id=:id");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", form.getId());
		params.put("title", form.getTitle());
		params.put("content", form.getContent());
		params.put("url", form.getUrl());
		params.put("satart_age", form.getSatart_age());
		params.put("end_age", form.getEnd_age());
		params.put("mtype", form.getMtype());
		dao.executeSql(sql.toString(), params);
	}
	public void deleteUser(List<String> list){
		String sql = "delete from child_manual where id in (:ids)";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ids", list);
		dao.executeSql(sql, params);
	}
}
